package mysql

import (
	"fresh-box-mart/freshrpc/freshrpc"
	"gorm.io/gorm"
)

type Fresh struct {
	gorm.Model
	Title string  `gorm:"type:varchar(30);comment:标题"`
	Image string  `gorm:"type:varchar(100);comment:图片"`
	Price float64 `gorm:"type:decimal(10,2);comment:价格"`
	Typer string  `gorm:"type:varchar(30);comment:分类"`
}

func (f *Fresh) FreshAdd(db *gorm.DB) error {
	return db.Debug().Create(&f).Error
}

func (f *Fresh) FreshFind(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Find(&f).Error
}
func FreshList(db *gorm.DB, in *freshrpc.FreshListRequest) ([]*freshrpc.FreshList, error) {
	var list []*freshrpc.FreshList
	err2 := db.Debug().Model(&Fresh{}).Select(
		"id as fresh_id",
		"title",
		"image",
		"price",
		"typer",
	).Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list).Error
	return list, err2
}
